const util = require('../../utils/util')
const app = getApp();
Page({
  data:{
    api: app.globalData.api,
    hots: [],
    categorys: [],
    products: [],
    cid: 0,
    checkinfo: "",
  },
  onPullDownRefresh() { //下拉更新商品列表
    util.http('/product/index/vx?cid='+this.data.cid, resp=>{
      this.data.products = resp.products
      util.stopPullSetData(this)
    })
  },
  detail(e){
    //var 是局部便联合
    var item = e.currentTarget.dataset.item
    //存到本地直到软件卸载
    wx.setStorageSync('商品信息', item)
    //打开新的小程序页面
    util.redirect('detail')
  },
  onInput(e){
    this.data[e.currentTarget.dataset.name] = e.detail.value
    },
  checkCommodity(){
    if(this.data.checkinfo===""){
      return false;
    }
    var checkinfo = this.data.checkinfo
    console.log(checkinfo)
    wx.setStorageSync('查询内容', checkinfo)
    util.redirect('checkcommodity')
  },
  cateClick(e){ //点击类别出现商品
    this.data.cid = e.currentTarget.dataset.cid
    util.http('/product/index/vx?cid='+this.data.cid, resp=>{
      this.data.products = resp.products
      this.setData(this.data)
    })
  },
  onLoad(){
    util.http('/product/index/vx', resp=>{
      this.data.hots = resp.hots
      this.data.categorys = resp.categorys
      if(resp.categorys.length > 0){
        this.data.cid = resp.categorys[0].id
      }
      this.data.products = resp.products
      this.setData(this.data)  //将变化的数据更新
    })
  }
})